#!/usr/bin/python
# -*- coding:utf-8 -*-

"""
纠正任务状态
"""

from app.db.models.asset_tasks import AssetTasks


class ChangeAtEnableStatus(object):
    """
    除了web安全任务，其他任务settings中的开关一直为true， 页面上的开关只管理enableMonitor
    """

    def run(self):
        print("开始更新状态......")
        for at in AssetTasks.objects.find({"taskGroupType": {"$in": ['web_security', 'web_asset', 'http', 'ping']}}):
            if at.taskGroupType == "web_security":
                if any([at.vulSettings.enable and at.vulSettings.taskId,
                        at.sslSettings.enable and at.sslSettings.taskId,
                        at.contentSettings.enable and at.contentSettings.taskId,
                        at.securityEventSettings.enable and at.securityEventSettings.taskId,
                        ]):
                    at.enableMonitor = True
                else:
                    at.enableMonitor = False
            elif at.taskGroupType == "http":
                at.enableMonitor = at.httpSettings.enable
                at.httpSettings.enable = True
            elif at.taskGroupType == "ping":
                at.enableMonitor = at.pingSettins.enable
                at.pingSettins.enable = True
            elif at.taskGroupType == "web_asset":
                at.enableMonitor = at.assetSettings.enable
                at.assetSettings.enable = True
            at.save()
        print("更新状态结束！！！")
